﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Class : System.Web.UI.Page
{
    ILE_Global ileGlobal = new ILE_Global();
    ClassBLL classLogic = new ClassBLL();
    ClassStandardsBLL classStandardsLogic = new ClassStandardsBLL();

    protected int CourseID
    {
        get
        {
            return (int)ViewState["CourseID"];
        }
        set
        {
            ViewState["CourseID"] = value;
        }
    }

    protected int SectionNumber
    {
        get
        {
            return (int)ViewState["SectionNumber"];
        }
        set
        {
            ViewState["SectionNumber"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            try
            {
                CourseID = Convert.ToInt32(Request.QueryString.Get("CourseID"));
                SectionNumber = Convert.ToInt32(Request.QueryString.Get("SectionNumber"));
            }
            catch
            {
                // *SteveMsg
                string modalPopupMsg = "Invalid CourseID or SectionNumber.<br />You will be redirected to your My Classes page.";
                ModalPopupMessage.ShowModalPopup_Redirect("ILE Message", modalPopupMsg, 2, "MyClasses.aspx");
            }

            if (CourseID == 0 || SectionNumber == 0)
            {
                // *SteveMsg
                string modalPopupMsg = "Invalid CourseID or SectionNumber.<br />You will be redirected to your My Classes page.";
                ModalPopupMessage.ShowModalPopup_Redirect("ILE Message", modalPopupMsg, 2, "MyClasses.aspx");
                return;
            }

            Classes.CoursesRow course = classLogic.GetCourse(CourseID);
            Classes.Courses_SectionRow section = classLogic.GetSection(CourseID, SectionNumber);

            if (course == null || section == null)
            {
                // *SteveMsg
                string modalPopupMsg = "Invalid CourseID or SectionNumber.<br />You will be redirected to your My Classes page.";
                ModalPopupMessage.ShowModalPopup_Redirect("ILE Message", modalPopupMsg, 2, "MyClasses.aspx");
            }
            else
            {
                if (!IsClassEnabled())
                {
                    // *SteveMsg
                    string modalPopupMsg = "Invalid Access. This Course has been disabled.";
                    ModalPopupMessage.ShowModalPopup_Redirect("ILE Message", modalPopupMsg, 2, "MyClasses.aspx");
                    return;
                }
                lnkGoBackToMyClassesPage.NavigateUrl = "MyClasses.aspx";
                LoadCourseIntroduction();
                LoadCourseStandards();
            }
        }
    }

    protected void btnGoToEvaluation_Load(object sender, EventArgs e)
    {
        Button Sender = (Button)sender;
        EvaluationsBLL evaluationsLogic = new EvaluationsBLL();

        Evaluations._Relations_Evaluations_Courses_SectionDataTable relations = evaluationsLogic.GetRelationsByCourseIdAndSectionNumber(CourseID, SectionNumber);

        try
        {
            Sender.PostBackUrl = "Evaluation.aspx?EvaluationID=" + relations[0].EvaluationID.ToString() + "&CourseID=" + CourseID.ToString() + "&SectionNumber=" + SectionNumber.ToString();
        }
        catch
        {
            Sender.Visible = false;
        }
    }

    private bool IsClassEnabled()
    {
        Classes.CoursesRow course = classLogic.GetCourse(CourseID);
        Classes.Courses_SectionRow section = classLogic.GetSection(CourseID, SectionNumber);

        if (course.IsEnabled == true)
        {
            if (section.UseSectionInfo)
            {
                if (section.IsEnabled)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return true;
            }
        }
        else
        {
            return false;
        }
    }

    private void LoadCourseIntroduction()
    {
        Classes.CoursesRow course = classLogic.GetCourse(CourseID);
        Classes.Courses_SectionRow section = classLogic.GetSection(CourseID, SectionNumber);


        if (section.UseSectionInfo != true)
        {
            // USE CLASS DEFAULT INFORMATION
            lblCourseTitle.Text = course.Title.ToString() + " - " + course.Subtitle.ToString();
            lblCourseSection.Text = "Section " + SectionNumber.ToString();
            lblCourseIntroduction.Text = course.Introduction.ToString();
        }
        else
        {
            // USE SECTION INFORMATION
            lblCourseTitle.Text = course.Title.ToString() + " - " + section.Subtitle.ToString();
            lblCourseSection.Text = "Section " + SectionNumber.ToString();
            lblCourseIntroduction.Text = section.Introduction.ToString();
        }

        Page.Title = course.Title.ToString() + " [Section " + SectionNumber.ToString() + "] - ILE";
    }

    private void LoadCourseStandards()
    {
        ClassStandards.Courses_StandardDataTable classStandards = classStandardsLogic.GetClassStandardsByCourseID(CourseID);

        foreach (ClassStandards.Courses_StandardRow classStandard in classStandards)
        {
            ClassStandards.Courses_Standard_SectionRow sectionStandard = classStandardsLogic.GetClassSectionStandard(CourseID, SectionNumber, classStandard.StandardID);

            if (sectionStandard != null)
            {
                if (sectionStandard.UseSectionInfo)
                {
                    HyperLink lnkStandard = new HyperLink();
                    Label lblStandardIntro = new Label();
                    Panel PanelStandard = new Panel();
                    switch (sectionStandard.StandardID)
                    {
                        case 1:
                            lnkStandard = lnkNeededInfo;
                            lblStandardIntro = lblNeededIntro;
                            PanelStandard = PanelNeededInfo;
                            break;
                        case 2:
                            lnkStandard = lnkAccessingInfo;
                            lblStandardIntro = lblAccessingIntro;
                            PanelStandard = PanelAccessingInfo;
                            break;
                        case 3:
                            lnkStandard = lnkEvaluatingInfo;
                            lblStandardIntro = lblEvaluatingIntro;
                            PanelStandard = PanelEvaluatingInfo;
                            break;
                        case 4:
                            lnkStandard = lnkUsingInfo;
                            lblStandardIntro = lblUsingIntro;
                            PanelStandard = PanelUsingInfo;
                            break;
                    }

                    if (sectionStandard.IsStandardEnabled)
                    {
                        lnkStandard.NavigateUrl = "Standard.aspx?CourseID=" + CourseID.ToString() + "&SectionNumber=" + SectionNumber.ToString() + "&StandardID=" + classStandard.StandardID.ToString();
                        PanelStandard.Visible = true;
                    }
                    else
                    {
                        lnkStandard.Enabled = false;
                        PanelStandard.Visible = false;
                    }

                    string introContent = sectionStandard.IntroContent.ToString();
                    introContent = System.Text.RegularExpressions.Regex.Replace(introContent, "<[^>]*>", String.Empty);

                    if (introContent.Length > 100)
                    {
                        lblStandardIntro.Text = introContent.Substring(0, 100) + "...";
                    }
                    else if (introContent.Length <= 3)
                        lblStandardIntro.Text = "Introduction has not yet been provided.";
                    else
                        lblStandardIntro.Text = introContent;
                }
                else
                {

                    HyperLink lnkStandard = new HyperLink();
                    Label lblStandardIntro = new Label();
                    Panel PanelStandard = new Panel();
                    switch (classStandard.StandardID)
                    {
                        case 1:
                            lnkStandard = lnkNeededInfo;
                            lblStandardIntro = lblNeededIntro;
                            PanelStandard = PanelNeededInfo;
                            break;
                        case 2:
                            lnkStandard = lnkAccessingInfo;
                            lblStandardIntro = lblAccessingIntro;
                            PanelStandard = PanelAccessingInfo;
                            break;
                        case 3:
                            lnkStandard = lnkEvaluatingInfo;
                            lblStandardIntro = lblEvaluatingIntro;
                            PanelStandard = PanelEvaluatingInfo;
                            break;
                        case 4:
                            lnkStandard = lnkUsingInfo;
                            lblStandardIntro = lblUsingIntro;
                            PanelStandard = PanelUsingInfo;
                            break;
                    }

                    if (classStandard.IsStandardEnabled)
                    {
                        lnkStandard.NavigateUrl = "Standard.aspx?CourseID=" + CourseID.ToString() + "&SectionNumber=" + SectionNumber.ToString() + "&StandardID=" + classStandard.StandardID.ToString();
                        PanelStandard.Visible = true;
                    }
                    else
                    {
                        //lnkStandard.Text += " <span style='font-size: 8pt;'>(disabled)</span>";
                        //lnkStandard.Enabled = false;
                        PanelStandard.Visible = false;
                    }

                    string introContent = classStandard.IntroContent.ToString();
                    introContent = System.Text.RegularExpressions.Regex.Replace(introContent, "<[^>]*>", String.Empty);

                    if (introContent.Length > 100)
                    {
                        lblStandardIntro.Text = introContent.Substring(0, 100) + "...";
                    }
                    else if (introContent.Length <= 3)
                        lblStandardIntro.Text = "Introduction has not yet been provided.";
                    else
                        lblStandardIntro.Text = introContent;
                }

            }
            else
            {
                HyperLink lnkStandard = new HyperLink();
                Label lblStandardIntro = new Label();
                Panel PanelStandard = new Panel();
                switch (classStandard.StandardID)
                {
                    case 1:
                        lnkStandard = lnkNeededInfo;
                        lblStandardIntro = lblNeededIntro;
                        PanelStandard = PanelNeededInfo;
                        break;
                    case 2:
                        lnkStandard = lnkAccessingInfo;
                        lblStandardIntro = lblAccessingIntro;
                        PanelStandard = PanelAccessingInfo;
                        break;
                    case 3:
                        lnkStandard = lnkEvaluatingInfo;
                        lblStandardIntro = lblEvaluatingIntro;
                        PanelStandard = PanelEvaluatingInfo;
                        break;
                    case 4:
                        lnkStandard = lnkUsingInfo;
                        lblStandardIntro = lblUsingIntro;
                        PanelStandard = PanelUsingInfo;
                        break;
                }

                if (classStandard.IsStandardEnabled)
                {
                    lnkStandard.NavigateUrl = "Standard.aspx?CourseID=" + CourseID.ToString() + "&SectionNumber=" + SectionNumber.ToString() + "&StandardID=" + classStandard.StandardID.ToString();
                    PanelStandard.Visible = true;
                }
                else
                {
                    //lnkStandard.Text += " <span style='font-size: 8pt;'>(disabled)</span>";
                    //lnkStandard.Enabled = false;
                    PanelStandard.Visible = false;
                }

                string introContent = classStandard.IntroContent.ToString();
                introContent = System.Text.RegularExpressions.Regex.Replace(introContent, "<[^>]*>", String.Empty);

                if (introContent.Length > 100)
                {
                    lblStandardIntro.Text = introContent.Substring(0, 100) + "...";
                }
                else if (introContent.Length <= 3)
                    lblStandardIntro.Text = "Introduction has not yet been provided.";
                else
                    lblStandardIntro.Text = introContent;
            }



        }

    }
}
